package com.onelap.bls.tc2.ble;

import android.app.Service;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.bluetooth.BluetoothManager;
import android.bluetooth.BluetoothProfile;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;

import com.blankj.utilcode.util.LogUtils;

import java.util.UUID;

public class BleService extends Service {

    private BleUtil bleUtil = null;
    private BluetoothGatt bluetoothGatt = null;
    private BleParams.DeviceConnectionState deviceConnectionState = BleParams.DeviceConnectionState.STATE_DISCONNECTED;

    public BluetoothGatt getBluetoothGatt() {
        return bluetoothGatt;
    }

    public void setBluetoothGatt(BluetoothGatt bluetoothGatt) {
        this.bluetoothGatt = bluetoothGatt;
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    private final IBinder mBinder = new LocalBinder();

    public class LocalBinder extends Binder {
        public BleService getService(BleUtil b) {
            bleUtil = b;
            return BleService.this;
        }
    }


    /**
     * 连接设备
     */
    public void connectionDeviceBluetooth(BluetoothDevice bluetoothDevice) {
        bluetoothGatt = bluetoothDevice.connectGatt(this, false, bluetoothGattCallback);
    }


    /**
     * 关闭连接
     */
    public void closeDeviceBluetooth() {
        if (bluetoothGatt != null) {
            bluetoothGatt.disconnect();
            bluetoothGatt.close();
            bluetoothGatt = null;
        }
    }

    public BluetoothGattCallback bluetoothGattCallback = new BluetoothGattCallback() {
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
            super.onConnectionStateChange(gatt, status, newState);
            LogUtils.i("onConnectionStateChange", status + "      " + newState);
            bleUtil.bleCallback.bleOnGattCallbackConnectionStateChange(gatt, status, newState);
        }

        @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            super.onServicesDiscovered(gatt, status);
            bleUtil.bleCallback.bleOnGattCallbackServicesDiscovered(gatt, status);
        }

        @Override
        public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
            super.onCharacteristicChanged(gatt, characteristic);
            bleUtil.bleCallback.bleOnGattCallbackCharacteristicChanged(gatt, characteristic);
        }

        @Override
        public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
            super.onCharacteristicRead(gatt, characteristic, status);
            bleUtil.bleCallback.bleOnGattCallbackCharacteristicRead(gatt, characteristic, status);
        }

        @Override
        public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
            super.onCharacteristicWrite(gatt, characteristic, status);
            bleUtil.bleCallback.bleOnGattCallbackCharacteristicWrite(gatt, characteristic, status);
        }
    };
}
